#include <iostream>
#include <microhttpd.h>
#define PORT 8888
// HTTP请求处理函数
int request_handler(void *cls, struct MHD_Connection *connection,
                    const char *url, const char *method, const char *version,
                    const char *upload_data, size_t *upload_data_size, void **con_cls)
{
    const char *response = "Hello, World!";
    struct MHD_Response *resp;
    int ret;
    // 构造HTTP响应
    resp = MHD_create_response_from_buffer(strlen(response), (void*)response, MHD_RESPMEM_PERSISTENT);
    if (!resp) return MHD_NO;
    // 发送HTTP响应
    ret = MHD_queue_response(connection, MHD_HTTP_OK, resp);
    MHD_destroy_response(resp);
    return ret;
}
int webServer()
{
    struct MHD_Daemon *daemon;
    // 启动HTTP服务器
    daemon = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY, PORT, NULL, NULL,
                              &request_handler, NULL, MHD_OPTION_END);
    if (!daemon) return 1;
    std::cout << "Server started on port " << PORT << std::endl;
    // 等待HTTP请求
    getchar();
    // 停止HTTP服务器
    MHD_stop_daemon(daemon);
    return 0;
}
